package com.yueqianshop.coupon.service.impl;

import com.yueqianshop.coupon.entity.SeckillSkuRelationEntity;
import com.yueqianshop.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yueqianshop.common.utils.PageUtils;
import com.yueqianshop.common.utils.Query;

import com.yueqianshop.coupon.dao.SeckillSessionDao;
import com.yueqianshop.coupon.entity.SeckillSessionEntity;
import com.yueqianshop.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取最近三天需要秒杀的商品
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getLates3DaysSession() {

        List<SeckillSessionEntity> seckillSessionEntities = baseMapper.selectList(new QueryWrapper<SeckillSessionEntity>().between("start_time", getStartTime(), getEndTime()));
        if(seckillSessionEntities != null && seckillSessionEntities.size() > 0){
            List<SeckillSessionEntity> sessionEntities = seckillSessionEntities.stream().map((seckillSession) -> {
                List<SeckillSkuRelationEntity> seckillSkuRelationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", seckillSession.getId()));
                seckillSession.setSeckillSkuRelationEntities(seckillSkuRelationEntities);
                return seckillSession;
            }).collect(Collectors.toList());
            return sessionEntities;
        }

        return null;
    }

    /**
     * 获取最近三天的开始时间
     * @return
     */
    private String getStartTime(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取最近三天的结束时间
     * @return
     */
    private String getEndTime(){
        LocalDate nowBehind2Days = LocalDate.now().plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(nowBehind2Days, max);
        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}